package org.jeecg.modules.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.erp.base.entity.ErpGoodsIngredientsReplace;
import org.jeecg.modules.erp.base.entity.ErpGoodsIngredientsReplaceInfo;
import org.jeecg.modules.erp.base.mapper.ErpGoodsIngredientsReplaceInfoMapper;
import org.jeecg.modules.erp.base.mapper.ErpGoodsIngredientsReplaceMapper;
import org.jeecg.modules.erp.base.service.IErpGoodsIngredientsReplaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 原料代替品管理
 * @Author: jeecg-boot
 * @Date:   2024-05-24
 * @Version: V1.0
 */
@Service
public class ErpGoodsIngredientsReplaceServiceImpl extends ServiceImpl<ErpGoodsIngredientsReplaceMapper, ErpGoodsIngredientsReplace> implements IErpGoodsIngredientsReplaceService {

	@Autowired
	private ErpGoodsIngredientsReplaceMapper erpGoodsIngredientsReplaceMapper;
	@Autowired
	private ErpGoodsIngredientsReplaceInfoMapper erpGoodsIngredientsReplaceInfoMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ErpGoodsIngredientsReplace erpGoodsIngredientsReplace, List<ErpGoodsIngredientsReplaceInfo> erpGoodsIngredientsReplaceInfoList) {
		ErpGoodsIngredientsReplace replace = this.getOne(new LambdaQueryWrapper<ErpGoodsIngredientsReplace>().eq(ErpGoodsIngredientsReplace::getMaterialCode, erpGoodsIngredientsReplace.getMaterialCode()));
		if(replace!=null){
			erpGoodsIngredientsReplace.setId(replace.getId());
		}
		this.saveOrUpdate(erpGoodsIngredientsReplace);
//		if(erpGoodsIngredientsReplaceInfoList!=null && erpGoodsIngredientsReplaceInfoList.size()>0) {
//			for(ErpGoodsIngredientsReplaceInfo entity:erpGoodsIngredientsReplaceInfoList) {
//				//外键设置
//				entity.setIngredientsId(erpGoodsIngredientsReplace.getId());
//				erpGoodsIngredientsReplaceInfoMapper.insert(entity);
//			}
//		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ErpGoodsIngredientsReplace erpGoodsIngredientsReplace,List<ErpGoodsIngredientsReplaceInfo> erpGoodsIngredientsReplaceInfoList) {
		erpGoodsIngredientsReplaceMapper.updateById(erpGoodsIngredientsReplace);
		
		//1.先删除子表数据
		erpGoodsIngredientsReplaceInfoMapper.deleteByMainId(erpGoodsIngredientsReplace.getId());
		
		//2.子表数据重新插入
		if(erpGoodsIngredientsReplaceInfoList!=null && erpGoodsIngredientsReplaceInfoList.size()>0) {
			for(ErpGoodsIngredientsReplaceInfo entity:erpGoodsIngredientsReplaceInfoList) {
				//外键设置
				entity.setIngredientsId(erpGoodsIngredientsReplace.getId());
				erpGoodsIngredientsReplaceInfoMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		erpGoodsIngredientsReplaceInfoMapper.deleteByMainId(id);
		erpGoodsIngredientsReplaceMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			erpGoodsIngredientsReplaceInfoMapper.deleteByMainId(id.toString());
			erpGoodsIngredientsReplaceMapper.deleteById(id);
		}
	}
	
}
